<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Order;
use App\Services\OrderService;
use Illuminate\Http\Request;

class PayController extends BaseController
{

    public function ali_notify(Request $request)
    {
        $alipay =  $this->payApp();
        try{
            $data = $alipay->verify(); // 是的，验签就这么简单！
            \Log::info('Alipay notify', $data->all());
            $out_trade_no = $data->out_trade_no;
            if (!in_array($data->trade_status, ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                throw new \Exception("支付状态异常");
            }
            $data->total_amount = $data->total_amount * 100;
            OrderService::pay_result($out_trade_no, $data->total_amount, $data->trade_no, Order::PAY_TYPE_ALI);
            return $alipay->success();
        } catch (\Exception $e) {
            \Log::error($e);
            return 'error';
        }
    }

    public function wx_notify(Request $request)
    {
        \Log::info('==========wx callback start ===============');
        $wx =  $this->payWx();
        try{
            $data = $wx->verify(); // 是的，验签就这么简单！
            \Log::info('wx notify', $data->all());
            $out_trade_no = $data->out_trade_no;
            if ($data->result_code != 'SUCCESS' || $data->return_code != 'SUCCESS') {
                throw new \Exception("支付状态异常");
            }

            $result = $this->payWx()->find(['out_trade_no' => $out_trade_no]);
            if (!in_array($result->trade_state, ['SUCCESS'])) {
                throw new \Exception($result->trade_state);
            }
            $out_trade_no = explode( "_", $out_trade_no)[0];
            OrderService::pay_result($out_trade_no, $data->total_fee, $data->transaction_id, Order::PAY_TYPE_WX);
            return $wx->success();
        } catch (\Exception $e) {
            \Log::error($e);
            return 'error';
        }
    }

    public function wx_refund_notify(Request $request)
    {
        \Log::info('==========wx refund callback start ===============');
        $wx =  $this->payWx();
        try{
            $data = $wx->verify(null, true); // 是的，验签就这么简单！
//            \Log::info('wx notify', $data->all());
            return $wx->success();
        } catch (\Exception $e) {
            \Log::error($e);
            return 'error';
        }
    }



    /**
     * SUCCESS—支付成功
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function wx_query(Request $request) {
        $this->validate($request, [
            'out_trade_no' => 'required'
        ]);
        try{
            $out_trade_no = \request('out_trade_no');
            $result = $this->payWx()->find(['out_trade_no' => $out_trade_no]);
            if (!in_array($result->trade_state, ['SUCCESS'])) {
                throw new \Exception($result->trade_state);
            }
            $prefix = substr($out_trade_no, 0, 1);
            OrderService::pay_result($out_trade_no, $result->total_fee, $result->transaction_id, Order::PAY_TYPE_WX);
            $map = ['SUCCESS' => 0, 'NOTPAY' => 1, 'CLOSED' => 3];
            $data['code'] = $map[$result->trade_state] ?? 9;
            return $this->success($data);
        } catch (\Exception $e) {
            \Log::error($e);
            $data['code'] = 9;
            return $this->failed($data);
        }
    }



    public function ali_query(Request $request) {
        $this->validate($request, [
           'out_trade_no' => 'required'
        ]);
        $out_trade_no = \request('out_trade_no');
        $alipay_url = $this->payApp()->find($out_trade_no);
        dd($alipay_url);
    }


}